package co.yixiang.modules.adopt.serviceimpl;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.exception.BadRequestException;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.FileUtil;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.adopt.entity.domain.YxAdoptVariety;
import co.yixiang.modules.adopt.entity.dto.YxAdoptVarietyQueryCriteria;
import co.yixiang.modules.adopt.mapper.AdoptVarietyMapper;
import co.yixiang.modules.adopt.service.YxAdoptCategoryService;
import co.yixiang.modules.adopt.service.YxAdoptVarietyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxAdoptVarietyServiceImpl extends BaseServiceImpl<AdoptVarietyMapper, YxAdoptVariety> implements YxAdoptVarietyService {
    @Autowired
    private IGenerator generator;

    @Autowired
    private YxAdoptCategoryService yxAdoptCategoryService;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxAdoptVarietyQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxAdoptVariety> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    //@Cacheable
    public List<YxAdoptVariety> queryAll(YxAdoptVarietyQueryCriteria criteria) {
        List<YxAdoptVariety> list = baseMapper.selectList(QueryHelpPlus.getPredicate(YxAdoptVariety.class, criteria));
        list.forEach(yxAdoptVariety -> {
            yxAdoptVariety.setAdoptCategory(yxAdoptCategoryService.getById(yxAdoptVariety.getCateId()));
        });
        return list;
    }

    @Override
    public void download(List<YxAdoptVariety> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxAdoptVariety yxAdoptVarietyDto : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("分类ID", yxAdoptVarietyDto.getCateId());
            map.put("品种名称", yxAdoptVarietyDto.getName());
            map.put("排序", yxAdoptVarietyDto.getSort());
            map.put("图标", yxAdoptVarietyDto.getPic());
            map.put("是否推荐", yxAdoptVarietyDto.getIsShow());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public YxAdoptVariety saveVariety(YxAdoptVariety adoptVariety) {
        if (adoptVariety.getAdoptCategory().getId() == null) {
            throw new BadRequestException("分类名称不能为空");
        }
        adoptVariety.setCateId(adoptVariety.getAdoptCategory().getId().toString());
        // 添加商品
        this.save(adoptVariety);
        return adoptVariety;
    }

    @Override
    public void updateVariety(YxAdoptVariety resources) {
        if (resources.getAdoptCategory() == null || resources.getAdoptCategory().getId() == null) {
            throw new BadRequestException("请选择分类");
        }
        resources.setCateId(resources.getAdoptCategory().getId().toString());
        this.saveOrUpdate(resources);
    }

    @Override
    public void delete(Integer id) {
        this.removeById(id);
    }

    @Override
    public boolean checkVarietyExist(String name, Integer id) {
        QueryWrapper<YxAdoptVariety> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        if (ObjectUtil.isNotEmpty(id)) {
            queryWrapper.ne("id", id);
        }
        final YxAdoptVariety one = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            return true;
        }
        return false;
    }

    @Override
    public List<YxAdoptVariety> selectListByCateId(Integer cateId) {
        return this.list(new LambdaQueryWrapper<YxAdoptVariety>().eq(YxAdoptVariety::getCateId, cateId));
    }

}
